package com.fenyin.samples.study.jdk.datastruct.queue;  
  
public class LinkedQueue<V> {

	private Entry<V> head;//头节点
	
	private Entry<V> tail;//尾指针
	
	private int size;
	
	public LinkedQueue(){
		head = new Entry<V>(null);
		tail = new Entry<V>(null);
		tail.next = head;
		size = 0;
	}
	
	/**
	 * 入队列
	 * @param value  
	 * @Description:
	 */
	public void enqueue(V value){
		Entry<V> curreny = new Entry<V>(value);
		
		tail.next.next = curreny;
		tail.next = curreny;
		size++;
	}
	
	/**
	 * 出队列
	 * @return  
	 * @Description:
	 */
	public V dequeue(){
		if(head == tail.next){
			throw new IndexOutOfBoundsException("队列已空");
		}
		V e = head.next.value;
		head.next = head.next.next;
		return e;
	}
	
	/**
	 * 队列大小
	 * @return  
	 * @Description:
	 */
	public int size(){
		return this.size;
	}
	
	public String display(){
		StringBuffer sb = new StringBuffer();
		if(head.next == null){
			return "";
		}
		for(Entry<V> curreny= head.next;curreny!=null;curreny = curreny.next){
			sb.append(curreny.value).append("|");
		}
		return sb.toString();
	}
	
	static class Entry<V>{
		V value;
		Entry<V> next;
		
		Entry(V value){
			this.value = value;
		}
	}
}
